const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../services/user.js');

//获取应用实例
const app = getApp()
Page({
  data: {
    regist: true,
    newGoods: [],
    hotGoods: [],
    topics: [],
    brands: [],
    floorGoods: [],
    banner: [],
    msgList: [],
    hotelList: [],
    sceneList: [],
    tourList: [],
    activitiList: [],
    channel: [],
    ad: "",
    couponList: [],
    showModal: 'hideModal',
    showMask: 'hideMask',
    // 页面配置  
    winWidth: 0,
    winHeight: 0,
    // tab切换 
    currentTab: 0,
    tabclass: '',
    //酒店页面索引
    hotelIndex: 1,
    //景点页面索引
    sceneIndex: 1,
    //线路页面索引
    tourIndex: 1,
    //分页大小
    pageSize: 6,
    custoemrType: "3",
    scrolly: false,
    dingzhi:[],
    jingxuan:[],
    swiperCurrent: 0
  },
  onShareAppMessage: function () {
    return {
      title: '风光鱼',
      desc: '陪你一起去旅行',
      path: '/pages/index/index-new'
    }
  },
  swiperChange: function (e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  }  ,
  //下拉刷新 
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载 //模拟加载 
    var that = this;
    setTimeout(function (e) {
      //刷新数据
      that.getIndexData(that);
      // complete 
      wx.hideNavigationBarLoading()
      //完成停止加载 
      wx.stopPullDownRefresh()
      //停止下拉刷新 
    });
  },
  getIndexData: function (that) {
    //let that = this;
    var customerId = wx.getStorageSync("customerId");
    customerId = customerId ? customerId : null;
    var pageSize = that.data.pageSize;
    var data = {
      customerId: customerId
    }
    util.request(api.IndexUrl, data).then(function (res) {
      if (res.code === '200') {
        var topicList = res.datas.activitiList;
        for (var i = 0; i < topicList.length; i++) {
          if (topicList[i].typeId == '1') {
            topicList[i]['url'] = "/pages/sceneTicketDetail/scenticket?id=" + topicList[i].valueId
          } else if (topicList[i].typeId == '0') {
            topicList[i]['url'] = "/pages/hotelRoomDetail/roominfo?id=" + topicList[i].valueId
          } else {
            topicList[i]['url'] = "/pages/tourDetail/travelline?id=" + topicList[i].valueId
          }
        }
        that.setData({
          // newGoods: res.data.newGoodsList,
          // hotGoods: res.data.hotGoodsList,
          // topics: res.data.topicList,
          // brand: res.data.brandList,
          channel: res.datas.categoryList,
          banner: res.datas.banner,
          msgList: res.datas.messageList,
          sceneList: res.datas.sceneList,
          hotelList: res.datas.hotelList,
          tourList: res.datas.tourList,
          activitiList: topicList,
          ad: res.datas.ad,
          couponList: res.datas.couponList
        });

        if (res.datas.ad != null && res.datas.ad != "" && !wx.getStorageSync("adShow")) {

          that.setData({
            showModal: 'showModal',
            showMask: 'showMask'
          });
          wx.setStorageSync("adShow", true);
        }


      }
    });
  },
  onLoad: function (options) {
    // wx.setStorageSync("adShow", false);
    // 获取系统信息 
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    //初始化数据
    app.init(options, this);
    this.setData({
      isShare: options.isShare ? options.isShare : false
    })


  },
  onReady: function () {
    // 页面渲染完成

  },
  onShow: function () {
    // 页面显示 
    this.setData({
      hotelIndex: 1,
      sceneIndex: 1,
      tourIndex: 1
    })

   this.getIndexData(this);
    this.getData(this);
    if (wx.getStorageSync("isShare")) {
      wx.showShareMenu({
        withShareTicket: true,
        success: function (e) {
          console.log(e)
          wx.setStorageSync("isShare", false);
        }
      })
    }
    app.getShareInfo(this);

  },
  onHide: function () {

  },
  onUnload: function () {
    // 页面关闭
  },
  //关闭广告
  hideAmountModal: function () {
    let that = this;
    wx.setStorageSync("ad_id", that.data.ad.id);
    that.setData({
      showModal: 'hideModal',
      showMask: 'hideMask',
    })
    wx.setStorageSync("adShow", "showed")
  },
  fnGetCon: function (e) {
    let that = this;
    var item = e.currentTarget.dataset.item;
    var index = parseInt(e.currentTarget.dataset.index);
    var couponList = that.data.couponList;
    //发起请求
    if (item.isHave == 1) {
      return
    }
    if (!wx.getStorageSync("customerId")) {
      wx.showModal({
        title: '您还未登录，登录后领取？',
        content: '',
        confirmText: "立即登录",
        cancelText: "稍后再说",
        success: function (options) {
          if (options.confirm) {
            wx.navigateTo({
              url: '/pages/auth/register/register',
            })
          }
        }
      });
      return
    }
    var data = {
      token: wx.getStorageSync("token"),
      id: item.id
    }
    util.request(api.CouponGet, data).then(function (res) {
      if (res.code === '200') {
        couponList[index].isHave = 1;
        that.setData({
          couponList: couponList
        });
      }
    });
  },
  fnMessageDetail: function (e) {
    var id = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.title;
    wx.navigateTo({
      url: '/pages/messageInfo/index?id=' + id + "&title=" + title,
    })
  },  // 滑动切换tab 
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });
  },
  // 点击tab切换 
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  //上拉加载更多
  fnGetMore: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    that.fnGetMoreData(type);
    that.setData({
      scrilly: false
    })
  },
  fnGetMoreData: function (type) {
    var that = this;
    var pageSize = that.data.pageSize;
    var pageNum = 0;
    var url = "";
    if (type == 'hotel') {
      url = api.IndexHotel;
      pageNum = that.data.hotelIndex + 1;
      that.setData({
        hotelIndex: pageNum
      })
    } else if (type == "scene") {
      url = api.IndexScene;
      pageNum = that.data.sceneIndex + 1;
      that.setData({
        sceneIndex: pageNum
      })
    } else {
      url = api.IndexTour;
      pageNum = that.data.tourIndex + 1;
      that.setData({
        tourIndex: pageNum
      })
    }
    wx.showLoading({
      title: '加载中...'
    });
    util.request(url, { pageSize: pageSize, pageNum: pageNum }).then(function (res) {
      wx.hideLoading();
      if (res.code == "200") {
        if (res.datas.hotels && res.datas.hotels.length > 0) {
          that.setData({
            hotelList: that.data.hotelList.concat(res.datas.hotels)
          })
        } else if (res.datas.scenes && res.datas.scenes.length > 0) {
          that.setData({
            sceneList: that.data.sceneList.concat(res.datas.scenes)
          })
        } else if (res.datas.tour && res.datas.tour.length > 0) {
          that.setData({
            tourList: that.data.tourList.concat(res.datas.tours)
          })
        } else {
          // wx.showToast({
          //   title: '没有更多了',
          //   image: '/static/images/icon_error.png',
          //   duration: 2000
          // });
          //减一页
          if (type == 'hotel') {
            that.setData({
              hotelIndex: pageNum - 1,
              noMore_hotel: true
            })
          } else if (type == 'scene') {
            that.setData({
              sceneIndex: pageNum - 1,
              noMore_scene: true
            })
          } else {
            that.setData({
              tourIndex: pageNum - 1,
              noMore_tour: true
            })
          }
          that.setData({

          });
        }
      } else {
        wx.showToast({
          title: '请求失败',
          image: '/static/images/icon_error.png'
        })
      }
    });
  },
  fnShare: function (e) {
    var that = this;
    var path = "";
    var type = e.currentTarget.dataset.type;
    var goodsId = e.currentTarget.dataset.id;
    var imgUrl = e.currentTarget.dataset.url;
    var goodsName = e.currentTarget.dataset.name;
    var address = e.currentTarget.dataset.address;
    var price = e.currentTarget.dataset.price;
    var marketPrice = e.currentTarget.dataset.marketprice;
    if (type == 'hotel') {
      path = "/pages/hotelDetail/hoteldetail";
    } else if (type == "scene") {
      path = "/pages/sceneDetail/sceninfo";
    } else {
      path = "/pages/tourDetail/travelline"
    }
    app.fnShareGoods(path, goodsId, imgUrl, goodsName, type, address, price, marketPrice);
  },
  upper: function () {
    this.setData({
      scrolly: true
    })
  }, adDetail: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    })
  },
  getData:function(){
    var that=this
    wx.showLoading({
      title: '加载中',
    })
    util.request(api.Commodity, { pageSize: that.data.pageSize, pageNum: 1, selfSupport:true }).then(function (res) {
      wx.hideLoading();
      if (res.code == "200") {
        that.setData({
          dingzhi: res.datas
        })
      }
    })
    util.request(api.Commodity, { pageSize: that.data.pageSize, pageNum: 1, selfSupport: false }).then(function (res) {
      wx.hideLoading();
      if (res.code == "200") {
        that.setData({
          jingxuan: res.datas
        })
      }
    })
  }
})
